//
//  LYProgressHUD.swift
//  1010
//
//  Created by 米明 on 15/7/25.
//  Copyright (c) 2015年 米明. All rights reserved.
//

import UIKit

class LYProgressHUD: UIView {
    
    static let shareEngine = LYProgressHUD()
    
    func setUp(message:String) {
        self.backgroundColor=UIColor.clear
        if messageLable != nil {
            messageLable.removeFromSuperview()
           self.removeFromSuperview()
        }
        setUpLabel(message: message)
    }
    
    func setUpLabel(message:String) {
        self.frame=CGRect(x: 0, y: 0, width: SCREEN_SIZE.width, height: SCREEN_SIZE.height)
        self.center.y -= 60
        
        let win = UIApplication.shared.keyWindow!
        win.addSubview(self)
        
        messageLable = produceLabel()
        messageLable.text = message
        show()
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.dismiss()
    }
    
    func produceLabel() -> UILabel {
        let label = UILabel(frame: CGRect(x: 0, y: 10, width: SCREEN_SIZE.width, height: 40))
        label.center=CGPoint(x: SCREEN_SIZE.width/2.0, y: 30)
        label.font = UIFont(name: FONT_HEIBAN, size: 24)
        label.textAlignment=NSTextAlignment.center
        label.textColor=LYColor.white
        self.addSubview(label)
        return label
    }
    
    var messageLable : UILabel!
    
    class func showMessage(message:String) {
        LYProgressHUD.shareEngine.setUp(message:message)
    }
    
    func show() {
        UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 0.66, initialSpringVelocity: 2.0, options: [], animations: {
            self.center.y += 60
        }) { (finished) -> Void in
            
        }
    }
    
    func dismiss() {
        UIView.animate(withDuration: 0.6, delay: 0, options: [], animations: {
            self.center.y -= 60
        }) { (finished) -> Void in
            self.messageLable.removeFromSuperview()
            
            self.removeFromSuperview()
            
        }
    }
}
